# %%
import numpy as np
import matplotlib.pyplot as plt

# %% == moon =====================================================
from sklearn.datasets import make_moons
from sklearn.preprocessing import StandardScaler
from util import operate_csv
from sklearn.decomposition import PCA
from sklearn.decomposition import KernelPCA

n_samples = 5000
x2, y2 = make_moons(n_samples=n_samples, random_state=89)

x_y = np.hstack((x2, y2.reshape(n_samples, 1)))
operate_csv.writeToCsv("D:/PyCharmProjects/MainProjects/anaconda-numpy/降维/data/moon.csv", x_y)

sc = StandardScaler()
x2_std = sc.fit_transform(x2)

# x2_std[y2 == 0, 0]: y2为0的所有行,第0列
plt.scatter(x2_std[y2 == 0, 0], x2_std[y2 == 0, 1], color='red', marker='^', alpha=0.5)
plt.scatter(x2_std[y2 == 1, 0], x2_std[y2 == 1, 1], color='blue', marker='o', alpha=0.5)
plt.xlabel('PC1')
plt.ylabel('PC2')
plt.show()

pca = PCA(n_components=2)
x_spca = pca.fit_transform(x2_std)

fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(14, 6))
ax[0].scatter(x_spca[y2 == 0, 0], x_spca[y2 == 0, 1], color='red', marker='^', alpha=0.5)
ax[0].scatter(x_spca[y2 == 1, 0], x_spca[y2 == 1, 1], color='blue', marker='o', alpha=0.5)
ax[1].scatter(x_spca[y2 == 0, 0], np.zeros((int(n_samples / 2), 1)) + 0.02, color='red', marker='^', alpha=0.5)
ax[1].scatter(x_spca[y2 == 1, 0], np.zeros((int(n_samples / 2), 1)) + 0.02, color='blue', marker='o', alpha=0.5)
ax[0].set_xlabel('PC1')
ax[0].set_ylabel('PC2')
ax[1].set_ylim([-1, 1])
ax[1].set_yticks([])
ax[1].set_xlabel('PC1')
plt.show()

kpca = KernelPCA(n_components=2, kernel='rbf', gamma=15)
x_kpca = kpca.fit_transform(x2_std)

fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(14, 6))
ax[0].scatter(x_kpca[y2 == 0, 0], x_kpca[y2 == 0, 1], color='red', marker='^', alpha=0.5)
ax[0].scatter(x_kpca[y2 == 1, 0], x_kpca[y2 == 1, 1], color='blue', marker='o', alpha=0.5)
ax[1].scatter(x_kpca[y2 == 0, 0], np.zeros((int(n_samples / 2), 1)) + 0.02, color='red', marker='^', alpha=0.5)
ax[1].scatter(x_kpca[y2 == 1, 0], np.zeros((int(n_samples / 2), 1)) + 0.02, color='blue', marker='o', alpha=0.5)
ax[0].set_xlabel('PC1')
ax[0].set_ylabel('PC2')
ax[1].set_ylim([-1, 1])
ax[1].set_yticks([])
ax[1].set_xlabel('PC1')

plt.savefig('D:/PyCharmProjects/MainProjects/anaconda-numpy/test_scala/picture/kpca/moon.png', dpi=200)
plt.show()

# %% == newMoon =====================================================
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

xy = pd.read_csv("D:/PyCharmProjects/MainProjects/anaconda-numpy/降维/data/newMoon.csv", header=None)
y = np.array(xy[2]).astype(int)
X = np.array(xy.drop(columns=[2]))

n_samples = 5000

plt.scatter(X[y == 0, 0], X[y == 0, 1], color='red', marker='^', alpha=0.5)
plt.scatter(X[y == 1, 0], X[y == 1, 1], color='blue', marker='o', alpha=0.5)
plt.xlabel('PC1')
plt.ylabel('PC2')
plt.savefig('D:/PyCharmProjects/MainProjects/anaconda-numpy/test_scala/picture/kpca/newMoon.png', dpi=200)
plt.show()

fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(16, 6))
ax[0].scatter(X[y == 0, 0], X[y == 0, 1], color='red', marker='^', alpha=0.5)
ax[0].scatter(X[y == 1, 0], X[y == 1, 1], color='blue', marker='o', alpha=0.5)
ax[1].scatter(X[y == 0, 0], np.zeros((int(n_samples / 2), 1)) + 0.02, color='red', marker='^', alpha=0.5)
ax[1].scatter(X[y == 1, 0], np.zeros((int(n_samples / 2), 1)) + 0.02, color='blue', marker='o', alpha=0.5)

ax[0].set_xlabel('PC1')
ax[0].set_ylabel('PC2')
ax[1].set_ylim([-1, 1])
ax[1].set_yticks([])
ax[1].set_xlabel('PC1')

plt.savefig('D:/PyCharmProjects/MainProjects/anaconda-numpy/test_scala/picture/kpca/newMoon.png', dpi=200)
plt.show()

